package club.ruanx.spider.common;

import lombok.Data;
import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author 阮胜
 * @date 2018/11/26 18:47
 */
@Data
@Component
public final class GlobalThreadPool {
    /**
     * 默认开启线程数
     */
    private int corePoolSize = 10;
    /**
     * 线程池最大数量
     */
    private int maxPoolSize = 30;

    /**
     * 线程闲置存活时间
     */
    private int keepAliveTime = 10;
    /**
     * 内部维护的线程池
     */
    public ThreadPoolExecutor pool;

    public GlobalThreadPool() {
        pool = new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, TimeUnit.MINUTES
                , new LinkedBlockingDeque<>(), (ThreadFactory) Thread::new);
    }

    public void execute(Runnable runnable) {
        this.pool.execute(runnable);
    }

}
